package com.syni.mdd.yhd.common.all.service.impl;

import com.syni.mdd.sjb.common.component.utils.common.utils.RequestHeaderData;
import com.syni.mdd.sjb.common.component.utils.common.utils.StringUtils;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsUserOnlineDao;
import com.syni.mdd.yhd.common.all.entity.BmsUserOnline;
import com.syni.mdd.yhd.common.all.entity.tool.BmsCityAreaDict;
import com.syni.mdd.yhd.common.all.service.BmsUserOnlineService;
import com.syni.mdd.yhd.common.all.service.impl.tool.BmsCityAreaDictServiceImpl;
import com.syni.mdd.yhd.common.all.service.tool.BmsCityAreaDictService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @className
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
public class BmsUserOnlineServiceImpl implements BmsUserOnlineService {
    @Autowired
    BmsUserOnlineDao bmsUserOnlineDao;

    @Autowired
    BmsCityAreaDictService bmsCityAreaDictService;

    @Override
    public BmsUserOnlineDao getBaseDao() {
        return bmsUserOnlineDao;
    }

    @Override
    public void addUserOnlineRecord(String userIdentify, String locationCity, String locationDistrict, String addr, String longitude, String latitude, String phoneModel, String loginIp, Date nowTime) {
        BmsUserOnline bmsUserOnline = new BmsUserOnline();
        bmsUserOnline.setUserId(Integer.parseInt(userIdentify));
        bmsUserOnline.setCity(locationCity);
        bmsUserOnline.setDistrict(locationDistrict);
        bmsUserOnline.setAddress(addr);
        bmsUserOnline.setClient(RequestHeaderData.getRequestHeaderData().getInteger("client"));
        bmsUserOnline.setNewTime(new Date());
        bmsUserOnline.setLongitude(longitude);
        bmsUserOnline.setLatitude(latitude);
        bmsUserOnline.setPhoneModel(phoneModel);
        bmsUserOnline.setLoginIp(loginIp);
        bmsUserOnlineDao.save(bmsUserOnline);
    }

    @Override
    public BmsUserOnline addUserOnlineRecord(String userIdentify, String areaCode, String addr, String longitude, String latitude, String phoneModel, String loginIp, Date nowTime) {
        BmsUserOnline bmsUserOnline = new BmsUserOnline();
        bmsUserOnline.setUserId(Integer.parseInt(userIdentify));
        bmsUserOnline.setAreaCode(areaCode);
        if(StringUtils.isNotBlank(areaCode)){
            BmsCityAreaDict bmsCityAreaDict = bmsCityAreaDictService.getByCodeId(areaCode);
            if(bmsCityAreaDict != null){
                bmsUserOnline.setCity(bmsCityAreaDict.getCityName());
                bmsUserOnline.setDistrict(bmsCityAreaDict.getDistinctName());
            }
        }
        bmsUserOnline.setAddress(addr);
        bmsUserOnline.setClient(RequestHeaderData.getRequestHeaderData().getInteger("client"));
        bmsUserOnline.setNewTime(new Date());
        bmsUserOnline.setLongitude(longitude);
        bmsUserOnline.setLatitude(latitude);
        bmsUserOnline.setPhoneModel(phoneModel);
        bmsUserOnline.setLoginIp(loginIp);
        bmsUserOnlineDao.save(bmsUserOnline);
        return bmsUserOnline;
    }
}
